<?php

namespace Home\Controller;

use Think\Controller;
use Home\Common\IdCard;
use Home\Controller\CaptchaController;
use Think\Crypt;

class UserController extends Controller {

    public $userModel = null;
    public $mem = null;
    public $url = "http://www.678fu.com/api.php";

    public function __construct() {
        parent::__construct();
        $this->userModel = new \Home\Model\UserModel();
    }

    public function login() {
        if ($this->userModel->check_login() > 0) {
            $this->redirect("Index/index");
        }
        if (IS_AJAX) {
            $username = I('get.username', '', 'strip_tags,stripslashes');
            $password = I('get.password', '', 'strip_tags,stripslashes');
            $user = $this->userModel->getByUsername($username);

            if (!$user && $this->userModel->regex_username($username)) {
                $this->ajaxReturn(array(
                    'ret' => 6,
                    'msg' => '用户名不存在！'
                        ), 'jsonp');
            } else {
                if (!Crypt::Crypt_Hash_Compare($user['password'], $password)) {
                    $this->ajaxReturn(array(
                        'ret' => 7,
                        'msg' => '用户名或密码错误！'
                            ), 'jsonp');
                } else {
                    //登录成功
                    session("[start]");
                    session('userinfo', $user);
                    $this->userModel->set_login_info($user);
                    $this->userModel->set_login_cookie();
                    $this->ajaxReturn(array(
                        'ret' => 0,
                        'msg' => '登录成功！'
                            ), 'jsonp');
                }
            }
        }
        $backurl = $_SERVER['HTTP_REFERER'];
        if (empty($backurl)) {
            $this->redirect('Index/index');
        }
        $this->assign('backurl', $backurl);
        $this->display();
    }

    public function register() {
        if ($this->userModel->check_login() > 0) {
            $this->redirect("Index/index");
        }
        if (IS_AJAX) {
            $get = I('get.');
            //var_dump($get);
            extract($get);

            if (!$this->userModel->check_password($password)) {
                $this->ajaxReturn(array('ret' => 1, 'msg' => '6~12个字符，只允许数字、字母、下划线'), 'json');
            }

            if (!$this->userModel->compare_password($password, $repassword)) {
                $this->ajaxReturn(array('ret' => 2, 'msg' => '两次密码输入不一样'), 'json');
            }

            if (!$this->userModel->check_realname($realname)) {
                $this->ajaxReturn(array('ret' => 3, 'msg' => '真实姓名输入不正确'), 'json');
            }

            if (!IdCard::validation_filter_id_card($idCard)) {
                $this->ajaxReturn(array('ret' => 4, 'msg' => '身份证号不正确'), 'json');
            }

            $verify = new CaptchaController();
            if (!$verify->check_verify($captcha)) {
                $this->ajaxReturn(array('ret' => 5, 'msg' => '验证码错误'), 'json');
            }

            $get['platform'] = "248game";

            $temp = file_get_contents($this->url . "?s=register/index&" . http_build_query($get));
            $info = json_decode($temp, true);
            if ($info['ret'] == 0) {
                //注册操作
                if ($data = $this->userModel->create($info['user'])) {
                    if ($this->userModel->add($data)) {
                        session("[start]");
                        session('userinfo', $info['user']);
                        $this->userModel->set_login_cookie();
                        $this->ajaxReturn(array(
                            'ret' => 0,
                            'msg' => '注册成功！'
                        ));
                    } else {
                        $this->ajaxReturn(array(
                            'ret' => 6,
                            'msg' => '注册失败！请稍后在试！'
                        ));
                    }
                } else {
                    $this->ajaxReturn(array(
                        'ret' => 6,
                        'msg' => '注册失败！请稍后在试！'
                    ));
                }
            } else {
                $this->ajaxReturn($temp);
            }
        }
        $this->display();
    }

    public function login_out() {
        if ($this->userModel->login_out()) {
            $this->ajaxReturn(array('ret' => 12, 'msg' => '成功退出'), 'json');
        } else {
            $this->ajaxReturn(array('ret' => 13, 'msg' => '成功失败，请稍后在试！'), 'json');
        }
    }

    public function check($username) {
        $username = I('get.username', '', 'strip_tags');
        //echo $this->url.'/index.php?s=register/check/username/'.$username;exit;
        $info = file_get_contents($this->url . '?s=register/check/username/' . $username);
        $this->ajaxReturn(json_decode($info));
        if ($this->userModel->regex_username($username)) {
            if ($this->userModel->getByUsername($username)) {
                $this->ajaxReturn(array(
                    'flag' => FALSE,
                    'msg' => '用户名已经被注册'
                        ), 'json');
            } else {
                $this->ajaxReturn(array(
                    'flag' => TRUE,
                    'msg' => '用户名可以注册'
                        ), 'json');
            }
        } else {
            $this->ajaxReturn(array(
                'flag' => FALSE,
                'msg' => '长度6~12个字符，数字或字母组成，必须以字母开头。'
                    ), 'json');
        }
    }

    public function tg_login() {
        //用户名存在，直接登录，不存在则是注册
        $get['rid'] = intval($_GET['rid']);
        $get['password'] = I('get.pwd');
        $get['username'] = I('get.username');
        $get['platform'] = I('get.platform');
        $get['sign'] = md5($get['rid'] . $get['password'] . $get['username'] . $get['platform'] . "4a2024560102baa7bceb812f8938569a");
        $url = "http://www.678fu.com/api.php?s=tglogin&" . http_build_query($get);
        //$this->ajaxReturn($url, 'jsonp');
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        if ($result['ret'] == 0) {
            $userModel = new \Home\Model\UserModel();
            $user = $userModel->getByUsername($get['username']);
            if ($user) {
                //比较密码
                if (!Crypt::Crypt_Hash_Compare($user['password'], $get['password'])) {
                    $this->ajaxReturn(array(
                        'ret' => 2,
                        'msg' => '用户名或密码错误！'
                            ), 'jsonp');
                } else {
                    //登录成功
                    session("[start]");
                    session('userinfo', $user);
                    $this->userModel->set_login_info($user);
                    $this->userModel->set_login_cookie();
                    $this->ajaxReturn(array(
                        'ret' => 0,
                        'msg' => '登录成功！'
                            ), 'jsonp');
                }
            } else {
                $userinfo = $result['userinfo'];
                //走注册流程
//                $data['username'] = $result['username'];
//                $data['password'] = Crypt::Crypt_Hash_Create($get['password']);
//                $data['reg_ip'] = get_client_ip(0, true);
//                $data['reg_time'] = time();
//                $data['register_type'] = $rid;               
                if ($result = $userModel->data($result['userinfo'])->add()) {
                    //$user = $this->userModel->where("uid='%d'", $result)->find();
                    session("[start]");
                    session('userinfo', $userinfo);
                    $this->userModel->set_login_cookie();
                    $this->ajaxReturn(array(
                        'ret' => 0,
                        'msg' => '注册成功！'
                            ), 'jsonp');
                } else {
                    $this->ajaxReturn(array(
                        'ret' => 1,
                        'msg' => '网络忙，请稍后在试！',
                            ), 'jsonp');
                }
            }
        }
    }

}
